package com.atguigu.spzx.service.product.controller;

import com.atguigu.spzx.model.common.Result;
import com.atguigu.spzx.model.dto.h5.ProductSearchDto;
import com.atguigu.spzx.model.entity.brand.Brand;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.h5.ProductDetailsVo;
import com.atguigu.spzx.service.product.service.BrandService;
import com.atguigu.spzx.service.product.service.ProductService;
import com.atguigu.spzx.service.product.service.ProductSkuService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * title:
 * author: zgl
 * date: 2024/1/15
 * description：
 */
@RestController
@RequestMapping("/api/product")
@Tag(name = "商品管理模块")
public class ProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private ProductSkuService productSkuService;

    @Resource
    private BrandService brandService;


    @PutMapping("/updateSkuSaleNum/{skuId}/{skuNum}")
    @Operation(summary = "更新商品销量")
    public Result updateSkuSaleNum(@PathVariable Long skuId, @PathVariable Integer skuNum) {
        ProductSku productSku = productSkuService.getById(skuId);
        productSku.setSaleNum(productSku.getSaleNum() + skuNum);
        productSku.setStockNum(productSku.getStockNum() - skuNum);
        productSkuService.updateById(productSku);
        return Result.ok();
    }

    @Operation(summary = "根据skuId查询商品信息")
    @GetMapping("/getSkuById/{skuId}")
    public Result<ProductSku> getProductList(@PathVariable Long skuId) {
        ProductSku productSku = productSkuService.getById(skuId);
        return Result.ok(productSku);
    }

    @Operation(summary = "查询商品列表接口")
    @GetMapping("/{page}/{limit}")
    public Result getProductList(@PathVariable Integer page, @PathVariable Integer limit, ProductSearchDto searchDto) {

        PageInfo<ProductSku> pages = productService.getProductListByPage(page, limit, searchDto);
        return Result.ok(pages);
    }

    @Operation(summary = "查询全部品牌接口")
    @GetMapping("/brand/findAll")
    public Result findAllBrands() {
        List<Brand> brandList = brandService.findBrandList();
        return Result.ok(brandList);
    }

    @Operation(summary = "查询商品详情接口")
    @GetMapping("/item/{skuId}")
    public Result findBrandDetails(@PathVariable Long skuId) {
        ProductDetailsVo productDetailsVo = productService.findBrandDetails(skuId);
        return Result.ok(productDetailsVo);
    }
}
